from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from loguru import logger

from asset.utils.dao import pageQuery
from asset.mongoDao import userManageDao, assetManageDao


def userManage(request):

    # 查询用户
    if request.method == 'GET':
        id = request.GET.get("id")
        if id is None:
            # 返回所有用户
            userlist,total = userManageDao.getAll()
            return JsonResponse(userlist, safe=False, json_dumps_params={'ensure_ascii': False})
        else:
            user = userManageDao.getUserbyId(id)
            logger.debug(f'获取到id为：{id}')
            return JsonResponse(user,safe=False, json_dumps_params={'ensure_ascii': False})
    # 新增资产
    if request.method == 'POST':
        pass
    # 更新资产
    if request.method == 'UPDATE':
        pass
    # 删除资产
    if request.method == 'DELETE':
        pass
def assetManage(request):
    # 查询资产
    if request.method == 'GET':
        id = request.GET.get("id")
        if id is None:
            # 返回所有用户
            assetlist, total = assetManageDao.getAll()
            return JsonResponse(assetlist, safe=False, json_dumps_params={'ensure_ascii': False})
        else:
            user = userManageDao.getUserbyId(id)
            logger.debug(f'获取到id为：{id}')
            return JsonResponse(user, safe=False, json_dumps_params={'ensure_ascii': False})
    # 新增资产
    if request.method == 'POST':
        id = request.POST.get('id')
        username = request.POST.get('username')
        status = request.POST.get('status')
        date = request.POST.get('date')

    # 更新资产
    if request.method == 'UPDATE':
        pass
    # 删除资产
    if request.method == 'DELETE':
        pass
def search(request):
    tableName = 'asset'
    if request.method == 'GET':
        keyword = request.GET.get("keyword")
        filter = request.GET.get('filter')
        resultJson = {"data":[],"result":"ok","total":0}
        if keyword is None:
            pagelist,total = pageQuery(tableName)
            resultJson['data'] = pagelist
            resultJson['total'] = total
            return JsonResponse(resultJson, safe=False, json_dumps_params={'ensure_ascii': False})

        pageno = request.POST.get('pageno')
        pagesize = request.POST.get('pagesize')
        if pagesize is None:
            pagesize=20
        if pageno is None:
            pageno=1
        pagelist, total = pageQuery(tableName, {keyword: filter}, pageno, pagesize)
        logger.info("查询{}".format({keyword: filter}))
        result = {"data": pagelist, "result": "ok", "total": total}
        print(result)
        return JsonResponse(result, safe=False, json_dumps_params={'ensure_ascii': False})
    if request.method == 'POST':
        type = request.POST.get('type')
        keyword = request.POST.get('keyword')
        filter = request.POST.get('filter')
        pageno = request.POST.get('pageno')
        pagesize = request.POST.get('pagesize')
        # 数据列表，数据条数用来分页
        pagelist,total = pageQuery(type,{keyword:filter},pageno,pagesize)
        result = {"data":pagelist,"result":"ok","total":total}
        print(result)
        return JsonResponse(result, safe=False, json_dumps_params={'ensure_ascii': False})